<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/1
 * Time: 14:41
 */
namespace Admin\Model;
use IDCU\Model\RelationModel;

class FamilyModel extends RelationModel
{
    protected $_validate = array(
//        array('no','require','家庭编号必填',1,'regex',3),
        array('room_id','require','房间ID缺失',1,'regex',3),
        array('no','/^\d{2}$/','家庭编码格式不正确',0,'regex'),
        array('no','','家庭编码已经存在',2,'unique'),
        array('no,room_id','checkNo1','家庭编码已存在',0,'callback',1),
        array('no,id,room_id','checkNo2','家庭编码已存在',0,'callback',2),
        array('room_id','checkRoomId','该房间下尚有激活家庭，不能添加新家庭',1,'callback',1)
    );

    protected $_auto = array(
        array('create_time','time',1,'function'),
        array('status','1',1)
    );

    protected $_link = array(
        'FamilyMember' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'FamilyMember',
            'mapping_name' => 'family_member',
            'foreign_key' => 'family_id'
        ),
        'Customer' => array(
            'mapping_type' => self::MANY_TO_MANY,
            'class_name' => 'Customer',
            'mapping_name' => 'customers',
            'foreign_key' => 'family_id',
            'relation_foreign_key' => 'customer_id',
            'relation_table' => 'zh_family_member',
        ),
        'Room' => array(
            'mapping_type'  => self::BELONGS_TO,
            'class_name'    => 'Room',
            'foreign_key'   => 'room_id',
            'mapping_name'  => 'room'
        )
    );

//    public function _before_write(&$data)
//    {
//        parent::_before_write($data); // TODO: Change the autogenerated stub
//        if(!$data['owner_id']){
//            $customer_id = D('FamilyMember')->find($data['family_id'])['customer_id'];
//            $data['owner_id'] = $customer_id;
////            D('FamilyMember')->where(array('family_id'=>$data['family_id'],'customer_id'=>$data['customer_id']))->save(array('prefix'=>'户主'));
//        }
//    }

    public function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
        if($data['room_id']){
            $room = M('Room')->find($data['room_id']);
//            $data['full_parent_name'] = $room['full_parent_name'].$room['name'];
            $data['address'] = $room['full_parent_name'].$room['name'];
            $data['community_id'] = $room['community_id'];
            $data['zone_id'] = $room['zone_id'];
            $data['building_id'] = $room['building_id'];
            $data['unit_id'] = $room['unit_id'];
//            if($data['no']){
//                $data['no'] = $room['no'].$data['no'];
//            }
        }
        if($data['create_time'] && strpos($data['create_time'],'-'))
            $data['create_time'] = strtotime($data['create_time']);
        if($data['modify_time'] && strpos($data['modify_time'],'-'))
            $data['modify_time'] = strtotime($data['modify_time']);
        if($data['delete_time'] && strpos($data['delete_time'],'-'))
            $data['delete_time'] = strtotime($data['delete_time']);
    }

    public function _before_update(&$data, $options)
    {
        parent::_before_update($data, $options); // TODO: Change the autogenerated stub
        if($data['status'] == -1){
            $data['delete_time'] = fetchDate();
        }
        $family = $this->find($data['id']);
//        $room = M('Room')->find($family['room_id']);
//        $data['address'] = $room['full_parent_name'].$room['name'];
    }

    public function _after_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
        M('Room')->where(array('id'=>$data['room_id']))->setInc('children_count',1);
    }

    public function _after_delete($data, $options)
    {
        parent::_after_delete($data, $options); // TODO: Change the autogenerated stub
    }

    function checkRoomId($roomId){
        if($this->where(array('room_id'=>$roomId,'status'=>1))->find()){
            return false;
        }else{
            return true;
        }
    }

    function checkNo1($param){
        $families = $this->field('no')->where(array('room_id' => $param['room_id']))->select();
        foreach($families as $k=>$v){
            if(substr($v['no'],-2) == $param['no']){
                return false;
            }
        }
        return true;
    }

    function checkNo2($param){
        $families = $this->field('no')->where(array('room_id' => $param['room_id'],'id'=>array('neq',$param['id'])))->select();
        foreach($families as $k=>$v){
            if(substr($v['no'],-2) == $param['no']){
                return false;
            }
        }
        return true;
    }
}
